<template>
  <div>
    <el-dialog title="编辑规格参数" :visible.sync="dialogVisible" width="60%">
      <!-- 外层表单 -->
      <el-form
        :model="editForm"
        ref="editForm"
        label-width="100px"
        class="demo-dynamic"
      >
        <el-form-item
          v-for="(domain, index) in editForm.domains"
          :label="domain.title"
          :key="index"
          :prop="domain.title"
        >
          <div class="content-item">
            <el-input v-model="domain.title"></el-input
            ><el-button @click.prevent="addChildDomain(index)" class="del-btn"
              >添加子项</el-button
            >
            <el-button @click.prevent="removeDomain(index)" class="del-btn"
              >删除</el-button
            >
          </div>

          <!-- 内层子表单 -->
          <el-form-item
            v-for="(item, index2) in domain.children"
            :label="item.title"
            :key="index2"
            :prop="item.title"
          >
            <div class="content-item child">
              <el-input v-model="item.title"></el-input>
              <el-button
                @click.prevent="removeChildDomain(index, index2)"
                class="del-btn"
                >删除子项</el-button
              >
            </div>
          </el-form-item>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clearInner">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { updateParams } from "@/api";
export default {
  props: ["rowParamsData"],
  data() {
    return {
      dialogVisible: false,
      itemCatId: '',
      editForm: {
        domains: [
          {
            title: "",
            value: "",
            children: [
              {
                title: "",
                value: "",
              },
            ],
          },
        ],
      },
    };
  },
  methods: {
    // 提交表单
    submitForm() {
      this.$refs.editForm.validate(async (valid) => {
        if (valid) {
          let obj = {
            itemCatId: this.itemCatId,
            content: JSON.stringify(this.editForm.domains),
          };
          let res = await updateParams(obj);
          if (res.status === 200) {
            this.$message({
              type: "success",
              message: "修改成功",
            });
            this.dialogVisible = false;
            // 清空
            this.editForm.domains = [
              {
                title: "",
                value: "",
                children: [
                  {
                    title: "",
                    value: "",
                  },
                ],
              },
            ];
            // 刷新
            this.$parent.getParamsData(1);
          } else {
            this.$message({
                type: 'error',
                message: '修改失败'
            });
            return false;
          }
        }
      });
    },
    // 取消表单提交
    clearInner() {
      this.dialogVisible = false;
      this.editForm.domains = [
        {
          title: "",
          value: "",
          children: [
            {
              title: "",
              value: "",
            },
          ],
        },
      ];
    },
    resetForm() {
      this.$refs.editForm.resetFields();
    },
    removeDomain(index) {
      this.editForm.domains.splice(index, 1);
    },
    addDomain() {
      this.editForm.domains.push({
        title: "",
        value: "",
        children: [],
      });
    },
    addChildDomain(index) {
      this.editForm.domains[index].children.push({
        title: "",
        value: "",
      });
    },
    removeChildDomain(index, index2) {
      this.editForm.domains[index].children.splice(index2, 1);
    },
  },
  watch: {
    rowParamsData(val) {
      this.itemCatId = val.itemCatId;
      this.editForm.domains = JSON.parse(this.rowParamsData.paramData);
    },
  },
};
</script>

<style lang="less" scoped>
.content-item {
  display: flex;
  .del-btn {
    margin-left: 10px;
  }
}

.tree {
  margin-bottom: 10px;
}
.child {
  margin: 10px 0;
}
</style>
